package com.neusoft.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neusoft.model.BooksModel;
import com.neusoft.model.BorrowhistoryModel;
import com.neusoft.model.entity.Books;
import com.neusoft.model.entity.Borrow_history;
import com.neusoft.model.vo.BooksModelVO;
import com.neusoft.service.BooksService;
import com.neusoft.service.BorrowHistoryService;
import com.neusoft.service.UserService;
@Controller
@RequestMapping("/borrow")
public class BorrowHistoryController {
	@Autowired
	UserService userService;
	@Autowired
	BorrowHistoryService borrowhistoryservice;
	@Autowired 
	BooksService bookservice;
	
	
	@RequestMapping("/toborrow_his")
	public String search_borrowing(){
		
	
		return "borrow_history";
	}
	
	//查看用户的借阅历史
	@RequestMapping("/borrow_his")
//	@ResponseBody
	public String search_history(String id,Model model){
		BorrowhistoryModel borrowhistoryModel=new BorrowhistoryModel();
		List<Borrow_history> history_list = new ArrayList<Borrow_history>();
		history_list=borrowhistoryservice.borrow_history(id);
		/*model.addAttribute("history",history_list);
		model.addAttribute("size", history_list.size()-1);*/
		borrowhistoryModel.setHistory_list(history_list);
		borrowhistoryModel.setTotal(borrowhistoryservice.countBorrowhistory());
		System.out.println(history_list.size()+"fdf");
		model.addAttribute("history_list",history_list);
		return "borrow_history2";
	}
	
	
	//查看用户的当前借阅图书信息
		@RequestMapping("/borrowing")
		public String search_borrowing(String id,Model model){
			List<BooksModel> books = new ArrayList<BooksModel>();
			List<Books> tmp=userService.borrowing(id);
			for(int i=0;i<tmp.size();i++){
				
//				Date d = new Date();
//				Date date=tmp.get(i).getDeadline(); 
////						tmp.get(i).getLendTime());
//				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//				String currdate = format.format(d);
//				 
//		        Calendar ca = Calendar.getInstance();
//			        ca.add(tmp.get(i).getLendTime().get, 70);// num为增加的天数，可以改变的
//				        d = ca.getTime();
			long times=tmp.get(i).getDeadline().getTime()-tmp.get(i).getLendTime().getTime();
			long d = times/1000/60/60/24;//天
			BooksModel bookTmp=new BooksModel();
			if(d>70){
				bookTmp.setRenewable(false);
			}else{
				bookTmp.setRenewable(true);
			}
			bookTmp.setAuthor(tmp.get(i).getAuthor());
			bookTmp.setBookName(tmp.get(i).getBookName());
			bookTmp.setCallno(tmp.get(i).getCallno());
			
			bookTmp.setIsbn(tmp.get(i).getIsbn());
			bookTmp.setLanguage(tmp.get(0).getLanguage());
			SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
			 
				 
				String lendTimestr = formater.format(tmp.get(i).getLendTime());
				String deadlineStr= formater.format(tmp.get(i).getDeadline());
			bookTmp.setDateToString(lendTimestr);
				bookTmp.setDeadlineToString(deadlineStr);
//			bookTmp.setLendTime(tmp.get(i).getLendTime());
			bookTmp.setTag(tmp.get(i).getTag());
			bookTmp.setId(tmp.get(i).getId());
			books.add(bookTmp);
			}
			
			model.addAttribute("borrowbooks", books);
			model.addAttribute("size",books.size()-1);
			//传递books列表到jsp
			return "findborrowing2";
		}
	
		//查看用户的借阅历史升级版
		@RequestMapping("/borrow_his_levelup")
//		@ResponseBody
		public String search_history_levelup(BooksModelVO booksModelVO,Model model){
//			BooksModel bbb=new BooksModel();
//			bbb.setId(123);
//			booksModelVO.setBooksModel(bbb);
//			booksModelVO.setId("123");
			List<Books> history_list = new ArrayList<Books>();
			history_list=bookservice.findBooksByActiveId(booksModelVO);
//			System.out.println(history_list.size()+"dfd");
			model.addAttribute("history_list",history_list);
			return "borrow_history2";
			
		}	
}
